package ecom.beans;

import ecom.beans.dao.AccountDAO;
import ecom.beans.dao.ProductDAO;
import ecom.beans.dao.ProductcategoryDAO;
import ecom.beans.dao.ProductstoreDAO;
import ecom.beans.dao.PurchaseDAO;
import ecom.beans.dao.StockDAO;
import ecom.beans.dao.TransactionDAO;
import ecom.beans.dao.UserDAO;
import ecom.beans.exception.AccountBeanException;
import ecom.beans.exception.DAONotFoundException;
import ecom.beans.exception.DAONotUniqueException;
import ecom.beans.exception.UserBeanException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class EcomAdminUserBean {

    public AccountBean getAccountById(String iban) {
        return AccountDAO.getByIBAN(iban);
    }

    public void createAccount(String iban, Double balance) throws AccountBeanException, DAONotUniqueException {
        AccountBean account = new AccountBean(iban, balance);
        AccountDAO.saveAccount(account);
    }

    public Long createUser(String pseudo, String password, String email, boolean isAdmin, String iban, Double balance) throws UserBeanException, DAONotUniqueException, AccountBeanException {
        AccountBean account = new AccountBean(iban, balance);
        UserBean user = new UserBean(pseudo, password, email, isAdmin, account);

        if (AccountDAO.getByIBAN(account.getIban()) == null) {
            if (account.getIban().length() > 34 || account.getIban().length() < 34) {
                throw new AccountBeanException("IBAN must contain 34 characters");
            }
            String exception = "";
            boolean isException = false;
            if (UserDAO.getByPseudo(pseudo) != null) {
                exception = "This pseudo already exist";
                isException = true;
            }
            if (UserDAO.getByEmail(email) != null) {
                isException = true;
                if (exception.equals("")) {
                    exception = "This email already exist";
                } else {
                    exception = exception + ", this email already exist";
                }
            }
            if (pseudo.length() > 50) {
                isException = true;
                if (exception.equals("")) {
                    exception = "This pseudo should not exceed 50 characters";
                } else {
                    exception = exception + ", this pseudo should not exceed 50 characters";
                }
            }
            if (password.length() > 50) {
                isException = true;
                if (exception.equals("")) {
                    exception = "This password should not exceed 50 characters";
                } else {
                    exception = exception + ", this password should not exceed 50 characters";
                }
            }
            if (email.length() > 50) {
                isException = true;
                if (exception.equals("")) {
                    exception = "This email should not exceed 50 characters";
                } else {
                    exception = exception + ", this email should not exceed 50 characters";
                }
            }
            if (isException) {
                throw new UserBeanException(exception);
            }

            List objectList = new ArrayList();
            objectList.add(account);
            objectList.add(user);
            TransactionDAO.saveOrUpdateAllObjects(objectList);
        } else {
            throw new DAONotUniqueException("This account (iban : " + iban + " already exist");
        }
        return user.getIdUser();

    }

    public Long createUser(String pseudo, String password, String email, String adress, String postalCode, String city, String country, String currency, String tel, boolean isAdmin, String iban, Double balance) throws UserBeanException, DAONotUniqueException, AccountBeanException {
        AccountBean account = new AccountBean(iban, balance);
        UserBean user = new UserBean(pseudo, password, email, adress, postalCode, city, country, currency, tel, isAdmin, account);

        if (AccountDAO.getByIBAN(account.getIban()) == null) {
            if (account.getIban().length() > 34 || account.getIban().length() < 34) {
                throw new AccountBeanException("IBAN must contain 34 characters");
            }
            String exception = "";
            boolean isException = false;

            if (UserDAO.getByPseudo(user.getPseudo()) != null) {
                exception = "This pseudo already exist";
                isException = true;
            }
            if (UserDAO.getByEmail(user.getEmail()) != null) {
                isException = true;
                if (exception.equals("")) {
                    exception = "This email already exist";
                } else {
                    exception = exception + ", this email already exist";
                }
            }
            if (user.getPseudo().length() > 50) {
                isException = true;
                if (exception.equals("")) {
                    exception = "This pseudo should not exceed 50 characters";
                } else {
                    exception = exception + ", this pseudo should not exceed 50 characters";
                }
            }
            if (user.getPassword().length() > 50) {
                isException = true;
                if (exception.equals("")) {
                    exception = "This password should not exceed 50 characters";
                } else {
                    exception = exception + ", this password should not exceed 50 characters";
                }
            }
            if (user.getEmail().length() > 50) {
                isException = true;
                if (exception.equals("")) {
                    exception = "This email should not exceed 50 characters";
                } else {
                    exception = exception + ", this email should not exceed 50 characters";
                }
            }
            if (user.getAdress() != null) {
                if (user.getAdress().length() > 100) {
                    isException = true;
                    if (exception.equals("")) {
                        exception = "This adress should not exceed 100 characters";
                    } else {
                        exception = exception + ", this adress should not exceed 100 characters";
                    }
                }
            }
            if (user.getPostalCode() != null) {
                if (user.getPostalCode().length() > 10) {
                    isException = true;
                    if (exception.equals("")) {
                        exception = "This postalcode should not exceed 10 characters";
                    } else {
                        exception = exception + ", this postalcode should not exceed 100 characters";
                    }
                }
            }
            if (user.getCity() != null) {
                if (user.getCity().length() > 100) {
                    isException = true;
                    if (exception.equals("")) {
                        exception = "This city should not exceed 100 characters";
                    } else {
                        exception = exception + ", this city should not exceed 100 characters";
                    }
                }
            }
            if (user.getTel() != null) {
                if (user.getTel().length() > 10) {
                    isException = true;
                    if (exception.equals("")) {
                        exception = "This phone number should not exceed 10 characters";
                    } else {
                        exception = exception + ", this phone number should not exceed 10 characters";
                    }
                }
            }
            if (isException) {
                throw new UserBeanException(exception);
            }

            List objectList = new ArrayList();
            objectList.add(account);
            objectList.add(user);
            TransactionDAO.saveOrUpdateAllObjects(objectList);
        } else {
            throw new DAONotUniqueException("This account (iban : " + iban + " already exist");
        }
        return user.getIdUser();
    }

    public UserBean getUserByPseudoAndPassword(String pseudo, String password) throws DAONotFoundException {
        UserBean user = UserDAO.getByPseudoAndPassword(pseudo, password);
        if (user == null) {
            throw new DAONotFoundException("This user (" + pseudo + ") has not been found");
        }
        return user;
    }

    public void updateUser(Long idUser, String adress, String city, String password, String email, boolean isAdmin) throws DAONotFoundException {
        UserBean user = UserDAO.getById(idUser);
        if (user != null) {
            user.setAdress(adress);
            user.setCity(city);
            user.setEmail(email);
            user.setPassword(password);
            user.setIsAdmin(isAdmin);
            UserDAO.updateUser(user);
        } else {
            throw new DAONotFoundException("This user (iduser=" + idUser + ") has not been found");
        }
    }

    public void removeUser(Long idUser) throws DAONotFoundException {
        UserBean user = UserDAO.getById(idUser);
        if (user != null) {
            UserDAO.deleteUser(user);
        } else {
            throw new DAONotFoundException("This user (iduser=" + idUser + ") has not been found");
        }
    }

    public void removeUser(String pseudo, String password) throws DAONotFoundException {
        UserBean user = UserDAO.getByPseudoAndPassword(pseudo, password);
        if (user != null) {
            List objects = new ArrayList();
            objects.add(user);
            if (user.getAccount() != null) {
                objects.add(user.getAccount());
            }
            TransactionDAO.deleteAllObjects(objects);
        } else {
            throw new DAONotFoundException("This user (pseudo=" + pseudo + ") has not been found");
        }
    }

    public Set<ProductBean> getProducts(String order) {
        return ProductDAO.getAllProducts(order);
    }

    public Set<PurchaseBean> getLastSales(String order) {
        return PurchaseDAO.getLastSales(" Order by p.purchaseBeanPK.dateAchat desc");
    }

    public ProductBean getProductById(Long idProduct) throws DAONotFoundException {
        ProductBean product = ProductDAO.getByIdProduct(idProduct);
        if (product == null) {
            throw new DAONotFoundException("The product " + idProduct + " has not been found");
        }
        return product;
    }

    public StockBean getStockByIdProductstoreAndIdProduct(Long idProductStore, Long idProduct) throws DAONotFoundException {
        return StockDAO.getByIdProductstoreAndIdProduct(idProduct, idProductStore);
    }

    public Set<ProductBean> getProductsByNameOrDescription(String name, String description, String order) {
        return ProductDAO.getByNameOrDesciption(name, description, order);
    }

    public Set<ProductBean> getPurchasesByIdProductcategory(Long idProductcategory, String order) {
        return ProductDAO.getByIdProductcategory(idProductcategory, order);
    }

    public Set<ProductstoreBean> getProductstores(String order) {
        return ProductstoreDAO.getAllProductstores(order);
    }

    public Set<ProductcategoryBean> getAllCategories(String order) {
        return ProductcategoryDAO.getAllProductcategories(order);
    }

    public ProductcategoryBean getProductcategoryById(Long idProductcategory) throws DAONotFoundException {
        ProductcategoryBean productcategory = ProductcategoryDAO.getByIdProductcategory(idProductcategory);
        if (productcategory == null) {
            throw new DAONotFoundException("The productcategory " + idProductcategory + " has not been found");
        }
        return productcategory;
    }

    public Set<StockBean> getStocks(String order) {
        return StockDAO.getAllStocks(order);
    }

    public Set<StockBean> getStockByIdProduct(Long idProduct, String order) {
        return StockDAO.getByIdProduct(idProduct, order);
    }

    public void removeAccount(String iban) throws AccountBeanException, DAONotUniqueException {
        AccountDAO.deleteAccount(iban);
    }

    public Set<ProductBean> getProductsByIdProductcategory(Long idProductcategory, String order) {
        return ProductDAO.getByIdProductcategory(idProductcategory, order);
    }

    public Set<ProductBean> getProductsByNameOrDesciption(String name, String description, String order) {
        return ProductDAO.getByNameOrDesciption(name, description, order);
    }

    public Set<ProductBean> getProductsByPriceInterval(Double firstPrice, Double secondPrice, String order) {
        return ProductDAO.getByPriceInterval(firstPrice, secondPrice, order);
    }

    public Set<ProductBean> getProductsByByMaximumPrice(Double maxPrice, String order) {
        return ProductDAO.getProductsByByMaximumPrice(maxPrice, order);
    }

    public ProductstoreBean getProductstoreById(Long idProductstore) throws DAONotFoundException {
        ProductstoreBean productstore = ProductstoreDAO.getByIdProductstore(idProductstore);
        if (productstore == null) {
            throw new DAONotFoundException("The productstore " + idProductstore + " has not been found");
        }
        return productstore;
    }

    public Set<PurchaseBean> getAllPurchases(String order) {
        return PurchaseDAO.getAllPurchases(order);
    }

    public Set<StockBean> getAllStocks(String order) {
        return StockDAO.getAllStocks(order);
    }

    public Set<StockBean> getBestStocks(String order, Integer limit) {
        return StockDAO.getBestTop(order, limit);
    }

    public Set<ProductstoreBean> getAllStores(String order) {
        return ProductstoreDAO.getAllProductstores(order);
    }

    public Set<ProductBean> getProductsByIdProductstore(Long idProductstore, String order) {
        return ProductDAO.getByIdProductstore(idProductstore, order);
    }

    public Set<StockBean> getStockByIdProductcategory(Long idProductcategory, String order) {
        return StockDAO.getByIdProductcategory(idProductcategory, order);
    }

    public Set<StockBean> getStockByIdProductcategoryAndIdProductstore(Long idProductcategory, Long idProductstore, String order) {
        return StockDAO.getByIdProductcategoryAndIdProductstore(idProductcategory, idProductstore, order);
    }

    public Set<StockBean> getStockByIdProductstore(Long idProductstore, String order) {
        return StockDAO.getByIdProductstore(idProductstore, order);
    }

    public Set<StockBean> getStockByProductNameOrDescription(String name, String description, String order) {
        return StockDAO.getByProductNameOrDescription(name, description, order);
    }
}
